<template>
  <div>
    <!-- 渲染角色列表 -->
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setRoles">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    roleId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 存放已选角色Id
      roleList: [] // 所有角色列表
    }
  },
  created() {
    // 获取所有角色
    this.getRoles()

    // 获取已有角色
    this.getUserDetailById()
  },
  methods: {
    // 点击确定
    async setRoles() {
      // 调接口
      const res = await assignRoles({
        id: this.roleId,
        roleIds: this.roleIds
      })
      // console.log(res)
      // 提示用户
      this.$message.success(res.message)
      // 关闭弹窗
      this.$emit('close')
    },

    // 取消角色分配
    cancleDialog() {
      this.$emit('close')
    },

    // 获取所有角色方法
    async getRoles() {
      const res = await getRoles()
      // console.log(res)
      this.roleList = res.data.rows
    },

    // 获取已有的角色 - 进行渲染
    async getUserDetailById() {
      const res = await getUserDetailById(this.roleId)
      console.log(res)
      this.roleIds = res.data.roleIds
    }
  }
}
</script>

<style scoped>
.footer {
  margin-top: 30px;
}
</style>
